package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.Role;
import com.dataseek.iot.sys.entity.RoleTable;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.RoleService;
import com.dataseek.iot.sys.vo.RoleVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private DbService dbService;

    /**
     * 查询角色列表
     *
     * @param roleName 角色名称
     * @param status 角色状态
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryRole(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size,@RequestParam(value="roleName", required=false) String roleName, @RequestParam(value="status", required=false)  String status) throws IOException {

        RoleVo roleVo = roleService.queryRole(roleName,status,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",roleVo);
    }

    /**
     * 修改角色列表
     *
     * @param roleTable 角色实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateRole(@RequestBody RoleTable roleTable) throws IOException {
        Integer code  = roleService.updateRole(roleTable);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建角色
     *
     * @param roleTable 角色实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertRole(@RequestBody RoleTable roleTable) throws IOException {
        Integer code  = roleService.insertRole(roleTable);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"角色名称重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }


    /**
     * 删除角色
     *
     * @param roleIds 角色ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteRole(@RequestBody JSONObject roleIds)  throws IOException {
        JSONArray jsonArray = roleIds.getJSONArray("ids");
        List<Integer> ids = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = roleService.deleteRole(ids);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"角色使用中，无法删除!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }





}
